﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace SlaktDataReader.Pages
{
    /// <summary>
    /// Interaction logic for pgMarriage.xaml
    /// </summary>
    public partial class pgMarriage : Page, INotifyPropertyChanged
    {
        private Book b;
        private MarriageBook bd;

        public Grid parentgrid;
        CollectionView view;

        public MarriageBook MarriageBookData
        {
            get { return bd; }
            set
            {
                if (bd != value)
                {
                    bd = value;
                    Notify("MarriageBookData");
                }
            }
        }
        public Book MarriageBook
        {
            get { return b; }
            set
            {
                if (b != value)
                {
                    b = value;
                    Notify("MarriageBook");
                }
            }
        }

        public pgMarriage()
        {
            InitializeComponent();

            bd = new MarriageBook();

            bd.ExportFail += new EventHandler(bd_ExportFail);
            bd.ExportSuccess += new EventHandler(bd_ExportSuccess);
            bd.ReadFail += new EventHandler(bd_ReadFail);
            bd.ReadSuccess += new EventHandler(bd_ReadSuccess);
        }

        void bd_ReadSuccess(object sender, EventArgs e)
        {
            Notify("MarriageBookData");
            view = (CollectionView)CollectionViewSource.GetDefaultView(lvMarriage.ItemsSource);

            view.Filter = _filter;
        }

        void bd_ReadFail(object sender, EventArgs e)
        {
            
        }

        void bd_ExportSuccess(object sender, EventArgs e)
        {
            
        }

        void bd_ExportFail(object sender, EventArgs e)
        {
            
        }


        private void Notify(string p)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(p));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void bback_Click(object sender, RoutedEventArgs e)
        {
            parentgrid.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void bApplyFilter_Click(object sender, RoutedEventArgs e)
        {
            FilterSearch();
            FilterWindow.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void bFilter_Click(object sender, RoutedEventArgs e)
        {
           
            FilterWindow.Visibility = Visibility.Visible;
        }

        private void FilterSearch()
        {
            CollectionViewSource.GetDefaultView(lvMarriage.ItemsSource).Refresh();
        }

        private bool _filter(object item)
        {
            try
            {
                MarriageBookRecord b = item as MarriageBookRecord;

                if (isnull(cfirst) && isnull(csur) && isnull(wfirst) && isnull(wsur) && isnull(mort) && isnull(fort) )
                    return true;

                bool rv = true;

                if (!isnull(cfirst))
                {
                    rv = (b.MaleFirstName.IndexOf(cfirst.Text, StringComparison.OrdinalIgnoreCase) >= 0);
                }

                if (!isnull(csur))
                {
                    rv &= (b.MaleSurName.IndexOf(csur.Text, StringComparison.OrdinalIgnoreCase) >= 0);
                }

                if (!isnull(wfirst))
                {
                    rv &= (b.FemaleFirstName.IndexOf(wfirst.Text, StringComparison.OrdinalIgnoreCase) >= 0);
                }
                if (!isnull(wsur))
                {
                    rv &= (b.FemaleSurName.IndexOf(wsur.Text, StringComparison.OrdinalIgnoreCase) >= 0);
                }

                if (!isnull(mort))
                {
                    rv &= (b.MaleAddress.IndexOf(mort.Text, StringComparison.OrdinalIgnoreCase) >= 0);
                }

                if (!isnull(fort))
                {
                    rv &= (b.FemaleAddress.IndexOf(fort.Text, StringComparison.OrdinalIgnoreCase) >= 0);
                }


                return rv;
            }
            catch
            { }
            return false;
        }

        private bool isnull(TextBox t)
        {
            return string.IsNullOrEmpty(t.Text);
        }
    }
}
